// #include "../common/message.hpp"
// #include "../common/net.hpp"
// #include "../common/dispatcher.hpp"
// #include "../server/rpc_router.hpp"

// void onRpcRequest(const RPC::BaseConnection::ptr& conn, RPC::RpcRequest::ptr& msg)
// {
//     std::cout << "收到了Rpc请求! " << msg->method() << std::endl;
//     std::string body = msg->serialize();
//     std::cout << body << std::endl;
//     auto rpc_req = RPC::MessageFactory::create<RPC::RpcResponse>();
//     rpc_req->setId("666");
//     rpc_req->setMType(RPC::MType::RSP_RPC);
//     rpc_req->setRCode(RPC::RCode::RCODE_OK);
//     rpc_req->setResult(33);
//     conn->send(rpc_req);
// }

// void onTopicRequest(const RPC::BaseConnection::ptr& conn, RPC::TopicRequest::ptr& msg)
// {
//     std::cout << "收到了Topic请求! " << std::endl;
//     std::string body = msg->serialize();
//     std::cout << body << std::endl;
//     auto rpc_req = RPC::MessageFactory::create<RPC::RpcResponse>();
//     rpc_req->setId("666");
//     rpc_req->setMType(RPC::MType::RSP_TOPIC);
//     rpc_req->setRCode(RPC::RCode::RCODE_OK);
//     rpc_req->setResult(33);
//     conn->send(rpc_req);
// }

#include "../../common/detail.hpp"
#include "../../server/rpc_server.hpp"


void Add(const Json::Value& req, Json::Value& rsp)
{
    int num1 = req["num1"].asInt();
    int num2 = req["num2"].asInt();
    rsp = (num1 + num2);
}

int main()
{
    //auto dispatcher = std::make_shared<RPC::Dispatcher>();
    // dispatcher->registerHandler<RPC::RpcRequest>(RPC::MType::REQ_RPC, onRpcRequest);
    // dispatcher->registerHandler<RPC::TopicRequest>(RPC::MType::REQ_TOPIC, onTopicRequest);
    std::unique_ptr<RPC::server::SDiscribeFactory> desc_factory(new RPC::server::SDiscribeFactory());
    desc_factory->setMethodName("Add");
    desc_factory->setParamsDesc("num1", RPC::server::VType::INTEGRAL);
    desc_factory->setParamsDesc("num2", RPC::server::VType::INTEGRAL);
    desc_factory->setReturnType(RPC::server::VType::INTEGRAL);
    desc_factory->setCallback(Add);
    // auto router = std::make_shared<RPC::server::RpcRouter>();
    // router->registerMethod(desc_factory->build());


    // auto cb = std::bind(&RPC::server::RpcRouter::onRpcRequest, router.get(),
    //     std::placeholders::_1, std::placeholders::_2);
    // dispatcher->registerHandler<RPC::RpcRequest>(RPC::MType::REQ_RPC, cb);


    // auto server = RPC::ServerFactory::create(9090);
    // auto message_cb = std::bind(&RPC::Dispatcher::onMessage, dispatcher.get(), 
    //         std::placeholders::_1, std::placeholders::_2);
    // server->setMessageCallback(message_cb);
    RPC::server::RpcServer server(RPC::Address("127.0.0.1", 9090));
    server.RegistryMethod(desc_factory->build());
    server.start();
    return 0;
}